/**
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.api.adwords.starterkit.examples;

import com.google.api.adwords.starterkit.StarterKit;
import com.google.api.adwords.starterkit.Synchronizer.SynchronizerType;
import com.google.api.adwords.starterkit.settings.StarterKitSettings;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * The Class StarterKitCmdLine. A command line demo application that allows the 
 * user to load/save content from/to AdWords account using the local StarterKit 
 * DB. Use this demo if you would like to develop you application in any other
 * language accessing StarterKit local DB.
 * 
 * @author rodrigo.f.vale@gmail.com (Rodrigo Vale)
 */
public class StarterKitCmdLine {
  
  /**
   * How to use.
   */
  public static void howToUse() {
    System.out.print(
        "SYNOPSIS\n" +
        "      StarterKit [COMMAND] [LOGIN] [PASSWORD] [CLIENT_LOGIN] " +
        "[DEVTOKEN] [USE_SANDBOX] [DB_DIR] [START_REPORT_DATE] " +
        "[END_REPORT_DATE]\n" +
        "\n" +
        "DESCRIPTION\n" +
        "      Command line application that allows the user to load/save " +
        "content from/to AdWords account using the local StarterKit DB.\n" +
        "Use this application if you don't want to code in Java or if you " +
        "want just to manipulate the DB to do all updates in the AdWords " +
        "Account.\n" +
        "\n" +
        "EXAMPLE\n" +
        "      java starterkit.jar StarterKit load_standard " +
        "INSERT_LOGIN_EMAIL_HERE INSERT_PASSWORD_HERE " +
        "INSERT_CLIENT_LOGIN_EMAIL_HERE INSERT_DEVELOPER_TOKEN_HERE false" +
        " ./ 12/01/2010 12/30/2010\n\n" +
        "      java starterkit.jar StarterKit load_severe " +
        "INSERT_LOGIN_EMAIL_HERE INSERT_PASSWORD_HERE " +
        "INSERT_CLIENT_LOGIN_EMAIL_HERE INSERT_DEVELOPER_TOKEN_HERE false " +
        "./ 12/01/2010 12/30/2010\n\n" +
        "      java starterkit.jar StarterKit save " +
        "INSERT_LOGIN_EMAIL_HERE INSERT_PASSWORD_HERE " +
        "INSERT_CLIENT_LOGIN_EMAIL_HERE INSERT_DEVELOPER_TOKEN_HERE false " +
        "./ 12/01/2010 12/30/2010\n\n");
     System.exit(0);
  }
  
  public static void main(String[] args) {
    if (args.length < 9) {
      howToUse();
    }
    
    String command = args[0];
    String login = args[1];
    String password = args[2];
    String clientLogin = args[3];
    String devToken = args[4];
    String sandbox = args[5];
    String dbDir = args[6];    
    String start = args[7];
    String end = args[8];
    
    StarterKitSettings starterKitSettings =  new StarterKitSettings();
        
    starterKitSettings.setLogin(login);
    starterKitSettings.setPassword(password);
    starterKitSettings.setClientLogin(clientLogin);
    starterKitSettings.setDevToken(devToken);
    starterKitSettings.setSandBox(sandbox.toLowerCase().equals("true"));
    starterKitSettings.setDBDir(dbDir);
    
    DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");    
    GregorianCalendar today = new GregorianCalendar();
    Date reportStart = today.getTime();
    Date reportEnd = today.getTime();
    try {
      reportStart = formatter.parse(start);
      reportEnd = formatter.parse(end);
    } catch (ParseException e) {
      e.printStackTrace();
    }  
    starterKitSettings.setReportStartDate(reportStart);
    starterKitSettings.setReportEndDate(reportEnd);
    
    if (command.equals("load") || command.equals("load_standard")) {
      starterKitSettings.setSyncType(SynchronizerType.STANDARD);
      StarterKit starterKit = new StarterKit(starterKitSettings);    
      starterKit.load();
      starterKit.done();
    } else if (command.equals("load_severe")) {
      starterKitSettings.setSyncType(SynchronizerType.DEEP);
      StarterKit starterKit = new StarterKit(starterKitSettings);    
      starterKit.load();
      starterKit.done();
    } else if (command.equals("save")) {
      starterKitSettings.setSyncType(SynchronizerType.STANDARD);
      StarterKit starterKit = new StarterKit(starterKitSettings);    
      starterKit.save();
      starterKit.done();
    }            
  }  
}